package cn.watson.system.controller;

import cn.watson.basic.utils.AjaxResult;
import cn.watson.basic.utils.PageList;
import cn.watson.system.domain.Detail;
import cn.watson.system.query.DetailQuery;
import cn.watson.system.service.IDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
// 此注解是@Controller+@ResponseBody两个注解的复合注解，这样整个接口类的所有接口方法全都是返回的JSON数据
@RequestMapping("/detail")
@Api(value = "数据明细的API",description="数据明细相关的CRUD功能")
public class DetailController {

    @Autowired
    private IDetailService detailService;

    /**
    * @Title: loadById
    * @Description: 根据id查询
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/29 23:52  
    * @Parameters: [id]
    * @Return cn.watson.system.domain.Detail
    */

    @GetMapping("/{id}") // 请求地址：http://localhost:8080/department/1 GET请求
    @ResponseBody   // 将前端传递的JSON对象数据转换为Java对象数据，
    public Detail loadById(@PathVariable("id") Long id){ // @PathVariable可以获取到接口路径中使用{}的占位符参数
        return detailService.loadById(id);
    }
    /*
    @GetMapping("/{types_id}")
    @ApiOperation(value = "通过types_id查询" )
    public List<Detail> loadByTypeId(@PathVariable("types_id") Long types_id){
        return detailService.loadByTypeId(types_id);
    }*/

    /**
     * @Title: remove
     * @Description: 更具id删除
     * @Author: ZhangRui
     * @Version: 1.0
     * @Date:  2022/6/24 11:54
     * @Parameters: [id]
     * @Return cn.watson.basic.utils.AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            detailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("我很忙的，等下再点！！");
        }
    }

    /**
    * @Title: removeByTypeId
    * @Description: 根据types_id删除
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/29 23:57  
    * @Parameters: [types_id]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @DeleteMapping("/typeId/{types_id}")
    @ApiOperation(value = "根据id删除" )
    public AjaxResult removeByTypeId(@PathVariable("types_id") Long types_id){
        try {
            detailService.removeByTypeId(types_id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }


    /**
    * @Title: addOrUpt
    * @Description: describe
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/29 10:58
    * @Parameters: [brandType]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @PutMapping
    @ApiOperation(value = "新增/修改" )
    public AjaxResult addOrUpt(@RequestBody Detail detail){
        try {
            if(detail.getId() == null){
                detailService.save(detail);
            }else {
                detailService.update(detail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙 错了！");
        }
    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/29 11:13
    * @Parameters: []
    * @Return java.util.List<cn.watson.system.domain.BrandType>
    */
    @GetMapping
    public List<Detail> loadAll(){
        return detailService.loadAll();
    }

    /**
     * @Title: queryData
     * @Description: 高级加分页查询
     * @Author: ZhangRui
     * @Version: 1.0
     * @Date:  2022/6/24 12:44
     * @Parameters: [departmentQuery]
     * @Return cn.watson.basic.utils.PageList<cn.watson.org.domain.Department>
     */
    @PostMapping
    @ApiOperation(value = "分页/高级查询" )
    public PageList<Detail> queryData(@RequestBody DetailQuery detailQuery){
        /*PageList<Detail> data = detailService.queryData(detailQuery);
        return data;*/
        return detailService.queryData(detailQuery);
    }

    /**
    * @Title: patchDel
    * @Description: describe
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/30 0:00  
    * @Parameters: [ids]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            detailService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙请重试！！！");
        }
    }


}
